﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Caching;
using Xmobile.DB;

namespace Xmobile.BO.Apps
{
    public class AppsBO
    {
        static int pageSize = 14;
        static int[] iCheck = new int[pageSize];
        private static void CheckAvailable(int index, int countRandom)
        {
            Random ran = new Random();
            int value = ran.Next(countRandom - 1);
            int count = iCheck.Length;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    if (iCheck[i] == value)
                    {
                        CheckAvailable(index, countRandom);
                    }
                }
            }
            iCheck[index] = value;
        }
        public static DataTable Apps_Select_by_CatID(int CatID)
        {
            string cacheName = SQLCache.CacheNameFormat.Apps_Select_by_CatID(CatID); 
            DataTable __result = null;
            __result = (DataTable)HttpContext.Current.Cache[cacheName];
            if (__result != null) return __result;

            using (MainDB db = new MainDB())
            {
                __result = db.StoredProcedures.Apps_Select_by_CatID(CatID);
                db.Close();
                db.Dispose();
            }

            if (__result != null)
            {
                string url_detail = "";
                __result.Columns.Add("url_detail");
                int count = __result.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    url_detail = "/" + __result.Rows[i]["Cat_DisplayUrlParent"].ToString() + "/" + __result.Rows[i]["AppCatID"].ToString() + "/" + __result.Rows[i]["AppID"].ToString() + "/" + Libs.UnicodeToKoDauAndGach(__result.Rows[i]["AppName"].ToString()) + Libs.Ext;
                    __result.Rows[i]["url_detail"] = url_detail;
                }

                __result.AcceptChanges();
            }

            System.Web.Caching.SqlCacheDependency __sqlDep = new SqlCacheDependency("Xmobile", "ContentCrawler");
            HttpContext.Current.Cache.Insert(cacheName, __result, __sqlDep);

            return __result;
        }

        public static DataTable Apps_Select_Ramdom(int CatID)
        {
            DataTable dt = new DataTable();
            DataTable __result = new DataTable();
            dt = Apps_Select_by_CatID(CatID);

            if (dt != null && dt.Rows.Count > 0)
            {
                int count = dt.Rows.Count;
                __result = dt.Clone();
                
                
                Random ran = new Random();
                for (int i = 0; i < pageSize; i++)
                {
                    CheckAvailable(i, count);
                    __result.ImportRow(dt.Rows[iCheck[i]]);
                }
                __result.AcceptChanges();
                
            }
            iCheck = new int[pageSize];
            return __result;
        }


        public static DataTable Apps_Select_by_AppID(int AppID)
        {
            string cacheName = SQLCache.CacheNameFormat.Apps_Select_by_AppID(AppID);
            DataTable __result = null;
            __result = (DataTable)HttpContext.Current.Cache[cacheName];
            if (__result != null) return __result;

            using (MainDB db = new MainDB())
            {
                __result = db.StoredProcedures.Apps_Select_by_AppID(AppID);
                db.Close();
                db.Dispose();
            }

         
            System.Web.Caching.SqlCacheDependency __sqlDep = new SqlCacheDependency("Xmobile", "ContentCrawler");
            HttpContext.Current.Cache.Insert(cacheName, __result, __sqlDep);

            return __result;
        }

        public static DataTable Apps_Select_List_Padding(int CatID, int PageIndex, int PageSize)
        {
            string cacheName = SQLCache.CacheNameFormat.Apps_Select_List_Padding(CatID, PageIndex, PageSize);
            DataTable __result = null;
            __result = (DataTable)HttpContext.Current.Cache[cacheName];
            if (__result != null) return __result;

            using (MainDB db = new MainDB())
            {
                __result = db.StoredProcedures.Apps_Select_List_Padding(CatID, PageIndex, PageSize);
                db.Close();
                db.Dispose();
            }

            if (__result != null)
            {
                string url_detail = "";
                __result.Columns.Add("url_detail");
                int count = __result.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    url_detail = "/" + __result.Rows[i]["Cat_DisplayUrlParent"].ToString() + "/" +__result.Rows[i]["AppCatID"].ToString() + "/" + __result.Rows[i]["AppID"].ToString() + "/" + Libs.UnicodeToKoDauAndGach(__result.Rows[i]["AppName"].ToString()) + Libs.Ext;
                    __result.Rows[i]["url_detail"] = url_detail;
                }

                __result.AcceptChanges();
            }

            System.Web.Caching.SqlCacheDependency __sqlDep = new SqlCacheDependency("Xmobile", "ContentCrawler");
            HttpContext.Current.Cache.Insert(cacheName, __result, __sqlDep);

            return __result;
        }

        public static void Apps_DownloadFile_AppID(int AppID)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.Apps_DownloadFile_AppID(AppID);
            }
        }

        public static void Apps_Insert(string AppName, string AppDes, string AppContent, string AppImage, string AppIcon, string AppLinkDownload, 
            string AppPartner, string AppSize, string AppDownloadCount, int AppCatId, string AppCategory)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.Apps_Insert(AppName, AppDes, AppContent, AppImage, AppIcon, AppLinkDownload, AppPartner, AppSize, AppDownloadCount, AppCatId, AppCategory);
            }
        }
    }
}
